/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package network.szyfrowanie;



import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import java.security.spec.InvalidParameterSpecException;

import javax.crypto.BadPaddingException; 
import javax.crypto.Cipher; 
import javax.crypto.IllegalBlockSizeException; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


/**
 *
 * @author mikolaj
 */
public abstract class  szyfrowanie {
    protected String idSesji;
    private final String d="AES/CFB/PKCS5Padding";
    byte[] iv;
    public String encrypt(String s,byte[] rawKey)
         throws NoSuchAlgorithmException, 
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException, InvalidParameterSpecException {
                byte[] input=s.getBytes();
                byte[] encrypted=encrypt(input,rawKey);
                return Base64.encode(encrypted);
    }
    
    public String decrypt(String s,byte[] rawKey) throws NoSuchAlgorithmException, 
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
        
		byte[] encrypted =Base64.decode(s);
                byte[] decrypted=decrypt(encrypted,rawKey);
                return new String(decrypted,"UTF-8");
    }
    

    
        public byte[] encrypt(byte[] s,byte[] rawKey)
         throws NoSuchAlgorithmException, 
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException, InvalidParameterSpecException {
		/*KeyGenerator kGen = KeyGenerator.getInstance("AES");
		kGen.init(256);
 
		SecretKey sKey = kGen.generateKey();*/   //Do generowania klucza
 
		SecretKeySpec sKeySpec = new SecretKeySpec(rawKey, "AES");
 
		// algorytm AES, tryb ECB, dopełnianie w PCKS#5
		Cipher cipher = Cipher.getInstance(d);
                if(iv!=null)
                    cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, new IvParameterSpec(iv));
                else{
                    cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
                    AlgorithmParameters parameters = cipher.getParameters();
                    iv=parameters.getParameterSpec(IvParameterSpec.class).getIV();
                }
		byte[] encrypted = cipher.doFinal(s);
                return encrypted;
    }
    
    public byte[] decrypt(byte[] s,byte[] rawKey) throws NoSuchAlgorithmException, 
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
        
		SecretKeySpec sKeySpec = new SecretKeySpec(rawKey, "AES");
		Cipher cipher = Cipher.getInstance(d);
                cipher.init(Cipher.DECRYPT_MODE, sKeySpec, new IvParameterSpec(iv));
                
                
		byte[] decrypted = cipher.doFinal(s);
        
        
                return decrypted;
    }
    
    public abstract boolean szyfruj(byte[] rawKey);
    
    public abstract boolean odszyfruj(byte[] rawKey);
    
}
